package com.example.cdctest.controller;
import com.example.cdctest.pojo.UserInfo;
import com.example.cdctest.pojo.WrongTopic;
import com.example.cdctest.service.QuestionService;
import com.example.cdctest.service.WrongTopicService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/services")
public class WrongTopicController {

    @Resource
    private WrongTopicService wrongTopicService;

    @RequestMapping("wrong_topic")
    public ModelAndView wrongTopic(HttpSession session, HttpServletRequest request) {
        ModelAndView modelAndView=new ModelAndView();
        UserInfo user = (UserInfo) session.getAttribute("loginUser");
       int id=user.getId();
     List<WrongTopic> wrongTopics= wrongTopicService.getWrongTopic(id);
        session.setAttribute("num",0);
        modelAndView.addObject("wrongTopics",wrongTopics);
        modelAndView.addObject("wrongCount",wrongTopicService.getWrongCount(id));
        modelAndView.setViewName("services/wrong_topic");
     return modelAndView;


    }
    @RequestMapping("xiayitiw")
    @ResponseBody
    public ModelAndView xiayitiq(HttpSession session)
    {
        UserInfo user = (UserInfo) session.getAttribute("loginUser");
        int id=user.getId();
        List<WrongTopic> wrongTopic= wrongTopicService.getWrongTopic(id);
        Integer num= (Integer) session.getAttribute("num");
        if (num<wrongTopic.size()-1)
        {
            num=num+1;
        }
        else {
         num=num;
        }

        session.setAttribute("num",num);
        ModelAndView modelAndView=new ModelAndView();

        modelAndView.addObject("questions",wrongTopic);
        modelAndView.setViewName("services/wrong_topic_manage");
        return modelAndView;
    }
    @RequestMapping("shangyitiw")
    @ResponseBody
    public ModelAndView shangyitiq(HttpSession session)
    {
        UserInfo user = (UserInfo) session.getAttribute("loginUser");
        int id=user.getId();
        List<WrongTopic> wrongTopic= wrongTopicService.getWrongTopic(id);
        Integer num= (Integer) session.getAttribute("num");
        if (num>0)
        {
            num=num-1;
        }
        else
        {
          num=num;
        }
        session.setAttribute("num",num);
        ModelAndView modelAndView=new ModelAndView();

        modelAndView.addObject("questions",wrongTopic);
        modelAndView.setViewName("services/wrong_topic_manage");
        return modelAndView;
    }
//答题卡
    @RequestMapping("answers-paper")
    @ResponseBody
    public ModelAndView answersPaper(Integer count,HttpSession session)
    {

        UserInfo user = (UserInfo) session.getAttribute("loginUser");
        int id=user.getId();
        List<WrongTopic> wrongTopic= wrongTopicService.getWrongTopic(id);
        Integer num= (Integer) session.getAttribute("num");
        num=count-1;
        session.setAttribute("num",num);

        ModelAndView modelAndView=new ModelAndView();

        modelAndView.addObject("questions",wrongTopic);
        modelAndView.setViewName("services/wrong_topic_manage");
        System.out.println(num);
        return modelAndView;
    }
}
